package com.myproject.shop.controller;

import com.myproject.shop.domain.Sales;
import com.myproject.shop.service.ISalesService;
import com.myproject.shop.util.AjaxResult;
import com.myproject.shop.util.BizException;
import io.swagger.annotations.Api;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/api/sales")
@Api(tags = "销售记录管理825王慧敏")
public class SalesController {

    @Autowired
    private ISalesService salesService;


    @ApiOperation("根据交易时间前后一小时查询销售记录825王慧敏")
    @ApiImplicitParam(name = "baseTime", value = "基准时间", dataType = "date", dataTypeClass = Date.class, paramType = "query")
    @GetMapping("/listByTimeRange")
    public AjaxResult listByTransactionTimeRange(
            @RequestParam(value = "baseTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date baseTime) {

        if (baseTime == null) {
            throw new BizException(500, "请指定基准时间");
        }

        List<Sales> salesList = salesService.selectSalesByTransactionTimeRange(baseTime);

        return AjaxResult.success(salesList);
    }


        @ApiOperation("添加销售记录信息825王慧敏")
        @PostMapping("/add")
        public AjaxResult addSales(@RequestBody Sales sales) {
            if (sales == null) {
                throw new BizException(500, "请传入有效的销售记录信息");
            }

            boolean success = salesService.addSales(sales);
            if (success) {
                return AjaxResult.success("销售记录添加成功");
            } else {
                return AjaxResult.error("销售记录添加失败");
            }
        }


}